let baseurl = "https://dvpmajh0.lc-cn-n1-shared.com"
function $http(url,method="GET",data={}){
	return new Promise((resolve,reject)=>{
		uni.request({
			url:baseurl+url,
			method,
			header:{
				"X-LC-Id":"dvPMajh0JgK2uTwKqIeUfUCA-gzGzoHsz",
				"X-LC-Key":"6s3yDahrdtSxVviq7s2HnGFP",
				"Content-Type":"application/json",
			},
			data,
			success: (res) => {
				resolve(res.data)
			},
			fail:(err)=>{
				reject(err)
			}
		})
	})
}

function $get (url,data={}){
	return $http(url,'GET',data)
}

function $post(url,data={}){
	return $http(url,'POST',data)
}

function $put(url,data={}){
	return $http(url,'PUT',data)
}

function $Delete(url,data={}){
	return $http(url,'DELETE',data)
}

function myCache(key, value, seconds = 3600 * 24) {
	let nowTime = Date.parse(new Date()) / 1000;
	if (key && value) {
		let expire = nowTime + Number(seconds);
		uni.setStorageSync(key,JSON.stringify(value) + '|' +expire)
		console.log('已经把' + key + '存入缓存,过期时间为' + expire)
	} else if (key && !value) {
		let val = uni.getStorageSync(key);
		if (val) {
			// 缓存存在，判断是否过期
			let temp = val.split('|')
			if (!temp[1] || temp[1] <= nowTime) {
				uni.removeStorageSync(key)
				console.log(key + '缓存已失效')
				return '';
			} else {
				return JSON.parse(temp[0]);
			}
		}
	}

}

module.exports = {
	$http,
	$get,
	$post,
	$put,
	$Delete,
  myCache:myCache
}



